package com.yycx.module.bus.provider.handler.process.task;

import com.yycx.bpm.provider.listener.FlowPageInterceptor;
import com.yycx.bpm.provider.model.TaskResponse;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.DateUtils;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.module.admin.client.entity.BaseUser;
import com.yycx.module.admin.provider.service.BaseUserService;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.BusInvoiceunit;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.client.entity.BusSupplier;
import com.yycx.module.bus.client.enums.AmountTypeEnum;
import com.yycx.module.bus.provider.service.*;
import org.flowable.common.engine.api.query.Query;
import org.flowable.task.api.TaskInfoQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 厂家返款(退垫付款)审批任务列表
 *
 * @author zyf
 */
@Component
public class RefundTaskTodoListHandler implements FlowPageInterceptor<TaskResponse> {

    @Autowired
    private BusPaymentService paymentService;
    @Autowired
    private BusSupplierBankService supplierBankService;
    @Autowired
    private BusSupplierService supplierService;

    @Autowired
    private BusDeptService deptService;

    @Autowired
    private BusCustomService customService;
    @Autowired
    private BaseUserService userService;
    @Autowired
    private BusOrderService orderService;

    @Autowired
    private BusInvoiceunitService invoiceunitService;

    @Autowired
    private BusInvoiceunitBankService invoiceunitBankService;

    @Override
    public void prepare(Query query, EntityMap params) {
        TaskInfoQuery taskQuery = (TaskInfoQuery) query;
        String companyId=OpenHelper.getCompanyId().toString();
        String paymentNo = params.get("paymentNo");
        BusPayment payment = paymentService.getByProperty("paymentNo", paymentNo);
        if (FlymeUtils.isNotEmpty(payment)) {
            taskQuery.processInstanceBusinessKey(payment.getPaymentId().toString());
        } else {
            taskQuery.processInstanceBusinessKey(paymentNo);
        }
        taskQuery.taskTenantId(companyId);
        taskQuery.processDefinitionKey("process_fankuan_"+companyId);
    }

    /**
     * 审批列表数据扩展
     *
     * @param query
     * @param data
     * @param params
     * @return
     */
    @Override
    public List<EntityMap> complete(Query query, List<TaskResponse> data, EntityMap params) {
        List<EntityMap> list = new ArrayList<>();
        if (FlymeUtils.isNotEmpty(data)) {
            for (TaskResponse taskResponse : data) {
                EntityMap map = new EntityMap();
                String paymentId = taskResponse.getBusinessKey();
                if (FlymeUtils.isNotEmpty(paymentId)) {
                    BusPayment payment = paymentService.getById(paymentId);
                    if (FlymeUtils.isNotEmpty(payment)) {
                        Long userId = payment.getUserId();

                        //单据类型
                        String amountType = payment.getAmountType();
                        BaseUser user = userService.getById(userId);
                        Long deptId = payment.getDeptId();

                        //厂家返款
                        if (AmountTypeEnum.Refund.getCode().equals(amountType)) {
                            getRefundMap(payment, map);
                        }
                        //厂家退保证金
                        if (AmountTypeEnum.SbReturn.getCode().equals(amountType)) {
                            getSbReturnMap(payment, map);
                        }
                        map.put("paymentNo", payment.getPaymentNo());
                        map.put("paymentId", payment.getPaymentId());
                        map.put("amountType", amountType);
                        map.put("amount", payment.getAmount());
                        map.put("nickName", user.getNickName());
                        map.put("createTime", payment.getCreateTime());
                        map.put("amountDate", DateUtils.formatDate(payment.getAmountDate(), "yyyy-MM-dd"));
                        map.put("paymentState", payment.getPaymentState());
                        map.put("assignee", taskResponse.getAssignee());
                        map.put("processInstanceId", taskResponse.getProcessInstanceId());
                        map.put("taskName", taskResponse.getName());
                        map.put("taskId", taskResponse.getId());
                        map.put("endTime", taskResponse.getEndTime());
                        map.put("orderId", payment.getOrderId());
                        if (FlymeUtils.isNotEmpty(deptId)) {
                            BusDept dept = deptService.getById(deptId);
                            map.put("deptName", dept.getDeptName());
                        }

                    }
                    list.add(map);
                }
            }
        }
        return list;
    }

    /**
     * 查询厂家返款信息
     *
     * @param payment
     * @param map
     */
    private void getRefundMap(BusPayment payment, EntityMap map) {
        Long invoiceUnitId = payment.getInvoiceUnitId();
        Long supplierId = payment.getSupplierId();
        //开票税金ID
        Long parentId = payment.getParentId();
        BusSupplier supplier = supplierService.getById(supplierId);
        BusInvoiceunit invoiceunit = invoiceunitService.getById(invoiceUnitId);
        BusPayment invoice = paymentService.getById(parentId);
        if (FlymeUtils.isNotEmpty(invoiceunit)) {
            map.put("unitName", invoiceunit.getInvoiceunitName());
        }
        if (FlymeUtils.isNotEmpty(supplier)) {
            map.put("supplierName", supplier.getSupplierName());
        }
        if (FlymeUtils.isNotEmpty(invoice)) {
            //开票金额
            map.put("invoiceAmount", invoice.getInvoiceAmount());
            map.put("invoiceDate", invoice.getAmountDate());
        }

        map.put("parentId", payment.getParentId());
    }

    /**
     * 查询厂家返款信息
     *
     * @param payment
     * @param map
     */
    private void getSbReturnMap(BusPayment payment, EntityMap map) {
        Long supplierId = payment.getSupplierId();
        //开票税金ID
        Long parentId = payment.getParentId();
        BusSupplier supplier = supplierService.getById(supplierId);
        if (FlymeUtils.isNotEmpty(supplier)) {
            map.put("supplierName", supplier.getSupplierName());
        }
    }
}
